// mircoApp/local_life/goods/goods.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    now:0,
    show: false,
    nums: 1,
    swiper_info:{thumb:[]},
    goodsInfo: {},
    time: 30 * 60 * 60 * 1000,
    timeData: {},
    language: "",
    info: {},
    goods_id:"",
    point:{
      lat:"",lng:""
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.goods_id = options.goods_id || '';
    let language = wx.getStorageSync("langIndex");
    let address_point = wx.getStorageSync("address-point");
    if (address_point) {
      this.data.point.lng = address_point.point.lng;
      this.data.point.lat = address_point.point.lat;
    }
    this.setData({
      language: language.en,
    });
    
  
    this.getData()
  },
  to_store(){
    wx.navigateTo({
      url: '/mircoApp/local_life/apply_store/apply_store?goods_id='+this.data.goods_id,
    })
  },
  getData() {
    let json = { goods_id: this.data.goods_id};
    if (this.data.point.lng) {
      json.latitude = this.data.point.lat * 1;;
      json.longitude = this.data.point.lng * 1;;
    }
    app._postNetWork({
      url: app.getNetAddresss("plugin.local-life.api.goods.detail"),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          let goodsInfo = data;
          this.setData({
            goodsInfo,
            "swiper_info.thumb":[data.has_one_goods.thumb]
          })
        } else {
          app.tips(msg);
        }
      }
    })
  },
  goto(){
    wx.navigateTo({
      url: '/mircoApp/local_life/store/index/index?id='+this.data.goodsInfo.store[0].store_id,
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  navigation(event) { //查看商家的地址定位信息
    let latitude = event.currentTarget.dataset.latitude;
    let longitude = event.currentTarget.dataset.longitude;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      name: this.data.storeDetail?.store_name,
      scale: 18
    });
  },
  call_phone() {
    let number = this.data.goodsInfo.store[0].tel;
    wx.makePhoneCall({
      phoneNumber: number,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  add() {
    let nums = this.data.nums + 1;
    this.setData({
      nums
    })
  },
  buy_handle() {
    if(this.data.goodsInfo.sale_start_at){
      if(this.data.goodsInfo.sale_start_at>this.data.now||this.data.now>this.data.goodsInfo.sale_end_at){
        return;
      }
    }
    wx.navigateTo({
      url: `/packageD/buy/myOrder_v2/myOrder_v2?goodsId=${this.data.goods_id}&total=${this.data.nums}&tag=local_life`
    })
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  popup_handle() {
    this.setData({
      show: true
    });
  },
  sign() {
    if (this.data.nums <= 1) {
      return
    }
    let nums = this.data.nums - 1;
    this.setData({
      nums
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      now: String(Date.parse(new Date())).substring(0, 10),
    });

  },
  finished(){
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})